<?php

/**
 * VanillaMediaImage form.
 *
 * @package    vanilla
 * @subpackage form
 * @author     Emiliano Nobis <emiliano.nobis@gmail.com>
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class VanillaMediaImageForm extends BaseVanillaMediaImageForm
{
  public function configure()
  {
        unset($this['created_at']);

        $this->setWidget('img', new sfWidgetFormInputFile(array('label' => 'Immagine')));

        $this->validatorSchema['img'] = new sfValidatorFile(array(
            'required' => true,
            'path' => sfConfig::get('sf_upload_dir').'/media',
            'mime_types' => 'web_images'
        ));

      $this->embedI18n(array('it', 'en'));
      $this->widgetSchema->setLabel('it', 'Italiano');
      $this->widgetSchema->setLabel('en', 'Inglese');
  }

  protected function processUploadedFile($field, $filename = null, $values = null) {
      $fn = parent::processUploadedFile($field, $filename, $values);

      if($fn != null) {
          $thumb = array('dir' => 'thumb', 'width' => '150', 'height' => '100');

          $thumbnail = new sfThumbnail($thumb['width'], $thumb['height'], false, true, 75, 'sfImageMagickAdapter', array('method' => 'shave_all'));
          $thumbnail->loadFile(sfConfig::get('sf_upload_dir')."/media/".$fn);
          $thumbnail->save(sfConfig::get('sf_upload_dir')."/media/".$thumb['dir']."/".$fn, 'image/jpeg');
      }

      return $fn;
  }
}
